package com.q.tqb.view.popwindow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.ShareInfo;
import com.q.tqb.util.CodeUtil;
import com.q.tqb.util.FileUtil;
import com.q.tqb.view.SocializeImageView;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * Created by Administrator on 2018/2/6.
 */

public class ImagePopWindow extends PopupWindow implements View.OnClickListener {

    private View popView;
    private Context mContext;
    private Bitmap mBitmap;
    private IWXAPI api;

    public ImagePopWindow(Context context, Bitmap bitmap) {
        this.mContext = context;
        this.mBitmap = bitmap;
        init();
    }

    private void init() {
        popView = View.inflate(mContext, R.layout.pop_image, null);
        api = WXAPIFactory.createWXAPI(mContext, AppConst.WEIXIN_APP_ID);

        TextView mTvSave = popView.findViewById(R.id.tv_save);
        TextView mTvShare = popView.findViewById(R.id.tv_share);
        TextView mTvCode = popView.findViewById(R.id.tv_code);

        mTvCode.setOnClickListener(this);
        mTvShare.setOnClickListener(this);
        mTvSave.setOnClickListener(this);

        //把View添加到PopWindow中
        this.setContentView(popView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.share_pop_anim);


        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha(0.6f);

        popView.setOnTouchListener(new View.OnTouchListener() {//设置背景区域外为点击消失popwindow
            public boolean onTouch(View v, MotionEvent event) {
                int height = popView.findViewById(R.id.ll_pop_share).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                if (mBitmap != null) {
                    FileUtil.saveImageToGallery(mContext, mBitmap);
                    Toast.makeText(mContext, "保存图片成功", Toast.LENGTH_SHORT).show();
                    backgroundAlpha(1.0f);
                    dismiss();

                }
                break;
            case R.id.tv_share:

                WXImageObject imgObj = new WXImageObject(mBitmap);
                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = imgObj;
                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("img");
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneSession;
                api.sendReq(req);
                backgroundAlpha(1.0f);
                dismiss();
                break;
            case R.id.tv_code:
                CodeUtil.analyzeBitmap(mBitmap, new CodeUtil.AnalyzeCallback() {
                    @Override
                    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                        Intent intent = new Intent(mContext, HtmlActivity.class);
                        intent.putExtra("url", result);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onAnalyzeFailed() {
                        Toast.makeText(mContext, "图片无效", Toast.LENGTH_LONG).show();
                    }
                });
                backgroundAlpha(1.0f);
                dismiss();
                break;
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
